#Kyle Bakken, Chelsea Evans, Adam Lamb - Group 6
#CS419-400
#Description: This program will filter a text document and fill an Appointment object 
#with details on an advisor appointment
#Input: This program takes a plain text document
#Output: This program will output an Appointment object to
    # 1. The SQL py script 
    # 2. The outlook py script


import os
import objects
import string
import re
import sys
import sqlhandler
from outlook_meeting import outlook_new, outlook_cancel

stringInput = ""

def getInput(input):
  stringInput = input

if len(stringInput) > 1:
  si = stringInput
else:
  si = sys.stdin
app = objects.Appointment#instantiate a new appointment object
#flags will be set according to app type
confirmflag = 0
cancelflag = 0

text = si.readline()
start = 0
end = text.find('\n')
app.advisor = text[start:end]#the first line is the advisors name
#print app.advisor #for testing

text = si.readline() 
start = text.find(' ') + 1
end = text.find('\n')
app.origin_email = text[start:end]
#print app.origin_email #for testing

text = si.readline()
start = text.find(' ') + 1
end = text.find('\n')
app.made = text[start:end]
#print app.made #for testing

text = si.readline()
start = text.find(';') + 2
end = text.find('\n')
app.advisor_email = text[start:end]
#print app.advisor_email #for testing

text = si.readline()
start = text.find(' ') + 1
end = text.find('\n') 
app.subject = text[start:end]
#print app.subject #for testing

while True:
    text = si.readline()
    if "confirmed" in text:
        app.app_type = "confirm"
        confirmflag = 1
        break
    if "CANCELLED" in text:
        app.app_type = "cancel"
        cancelflag = 1
        break
#print app.app_type#for testing

text = si.readline()
start = text.find(' ') + 1
end = text.find('\n')
app.advisee = text[start:end]
#print app.advisee   #for testing

text = si.readline()
start = text.find(' ') + 1
end = text.find('\n')
app.email = text[start:end]
#print app.email     #for testing

text = si.readline()
start = text.find(' ') + 1
end = text.find('\n')
app.date = text[start:end]
#print app.date  #for testing

text = si.readline()
start = text.find(' ') + 1
end = text.find('-') - 1
app.start_time = text[start:end]
start = text.find('- ') + 2
end = text.find('\n')
app.end_time = text[start:end]
#print app.start_time   #for testing
#print app.end_time #for testing

#see which flag is set
#call appropriate function
if cancelflag == 1:
    sqlhandler.DBUpdateCancel(app)
    outlook_cancel(app)
if confirmflag == 1:
    sqlhandler.DBInsertNew(app)
    outlook_new(app)

si.close()